var t;
var firsttel ='';
$(function(){

//第一次密码
$('#j_pwd_one').blur(function(){
   var pwd_1=$.trim($('#j_pwd_one').val());
   if(pwd_1.length >= 8){
      var pwd_2 = $.trim($('#j_pwd_two').val());
      if(pwd_2 != ''){
         if(pwd_1 != pwd_2){
            $('.pwd2_hide').html('两次密码不一致!');
            $('.pwd2_hide').css({"display":"inline"});
            $('.j_pie2').css({"display":"none"});
            passStatus=0;
         }else{
             $('.pwd1_hide').css({"display":"none"});
             $('.pwd1_img').css({"display":"inline"});
             $('.pwd2_hide').css({"display":"none"});
             $('.j_pie2').css({"display":"inline-block"});
             passStatus=1;
         }
      }else{
          $('.pwd1_hide').css({"display":"none"});
          $('.j_pie1').css({"display":"inline-block"});
          passStatus=1;
      }
   }else{
      $('.pwd1_hide').css({"display":"inline"});
      $('.j_pie1').css({"display":"none"});
      passStatus=0;
   }
    con();
});

//第二次密码
$('#j_pwd_two').blur(function(){
   var pwd_1 = $.trim($('#j_pwd_one').val());
   var pwd_2 = $.trim($('#j_pwd_two').val());
   if(pwd_1 != pwd_2){
      $('.pwd2_hide').html('两次密码不一致!');
      $('.pwd2_hide').css({"display":"inline"});
      $('.j_pie2').css({"display":"none"});
      passStatus=0;
   }else if(pwd_1 == pwd_2 && pwd_1 != "" && pwd_2 != ""){
      if(pwd_1.length < 8){
        $('.j_pie2').css({"display":"none"});
        $('.pwd2_hide').html('密码不能少于8位，建议字母数字组合!');
        $('.pwd2_hide').css({"display":"inline"});
        passStatus=0;
     }else{
         $('.j_pie2').css({"display":"inline-block"});
         $('.pwd2_hide').css({"display":"none"});
         passStatus=1;
     }
   }
    con();
});

 //各个按钮判断
 function con(){
     if(passStatus == 1 && telStatus == 1 && verifyStatus==1){
        $('#j_btn').css({"background":"#00a8ee","color":"#fff"});
        $('#j_btn').attr("onclick", 'Confirm()'); 
     }else{
        $('#j_btn').css({"background":"#cad0e2","color":"#777"});
        $('#j_btn').removeAttr("onclick");
     }
 }


// 邀请验证码
$("#j_input_yzm").blur(function(){   //邀请码验证
    var inviteCode = $.trim($('#j_input_yzm').val());
    if(!(/^[0-9]{6}$/.test(inviteCode))){
       $('.j_pie3').css({"display":"none"});
       verifyStatu=0;
    }else{ 
       $('.j_pie3').css({"display":"inline-block"});
       verifyStatu=1;
    }
    con();
});

//手机号
$('#j_tel').blur(function(){
   var moblie = $.trim($('#j_tel').val());
   if(!(/^(13|14|15|17|18)\d{9}$/.test(moblie))){
      $('#j_hq').html('请输入正确的手机号码!');
      $('#j_hq').css({"display":"inline"});
      $('#j_t_tel').css({"display":"none"});
      $('.j_button').removeAttr('onclick');
      $('.j_button').css({"background":"#cad0e2","color":"#777"});
      telStatus=0;
       con();
   }else{
      $.ajax({
         url:telurl,
         type: 'post',
         data: {tel:moblie},
         headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
         success: function(res){
              if(parseInt(res) === 1){
                  if(firsttel != moblie){
                      verifyStatus=0;
                      clearInterval(t);
                      $('.j_button').attr("value", '获取验证码');
                      $('.j_pie3').css({"display":"none"});
                      $('#j_t_tel').css({"display":"inline-block"});
                      $('#j_hq').css({"display":"none"});
                      $('.j_button').css({"background":"#00a8ee","color":"#fff"});
                      $('.j_button').attr('onclick','settime(this)');
                      $('.j_button').css({'disabled':true});
                  }
                 telStatus=1;
                  con();
              }else{
                  telStatus=0;
                  $('#j_hq').html('该手机号未注册!');
                  $('#j_hq').css({"display":"inline"});
                  $('#j_t_tel').css({"display":"none"});
                  $('.j_pie3').css({"display":"none"});
                  $('.j_button').removeAttr('onclick');
                  $('.j_button').css({'disabled':false});
                  $('.j_button').css({"cursor":'pointer'});
              }
             con();
         }
      });
   } 
});

//判断验证码
$('#j_input_yzm').blur(function(){
    var moblie = $.trim($('#j_tel').val());
    if(!(/^(13|14|15|17|18)\d{9}$/.test(moblie))){
       // $('.moblie_hide').css({"display":"inline"});
       // $('.moblie_img').css({"display":"none"});
       $('.j_button').css({"background":"#cad0e2","color":"#777"});
       verifyStatus=0;
        con();
    }else{
        var usercode=$.trim($('#j_input_yzm').val());
        if(usercode){
            $.ajax({
                url:checkverifycode,
                type: 'post',
                data: {code:usercode},
                dataType:'json',
                headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
                success: function(res){
                    if(parseInt(res.status) == 1){ //正确
                        $('.j_pie3').css({"display":"inline-block"});
                        verifyStatus=1;
                        con();
                    }else if(parseInt(res.status) == 0){
                        layer.msg('请输入正确的验证码!');
                        $('.j_pie3').css({"display":"none"});
                        verifyStatus=0;
                        con();
                    }
                }
            });
        }else{
            layer.msg('请输入验证码!');
            $('.j_pie3').css({"display":"none"});
            verifyStatus=0;
            con();
        }
    }

});

// function reday 结束
});


// var countdown=60;
function settime(obj) {
	var moblie=$.trim($('#j_tel').val());
  if(!(/^(13|14|15|17|18)\d{9}$/.test(moblie))){
     $('#j_hq').html('请输入正确手机号');
  }else{
     $.ajax({
          url:sendIdentify,
          type: 'post',
          data: {tel:moblie,type:1},
          dataType:'json',
          headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
          success: function(res){
              if(parseInt(res.status) == 1){
                 firsttel = moblie;
                 $(obj).removeAttr('onclick');
                 $(obj).css('background','#cad0e2');
                  $('#j_input_yzm').focus();
                  //console.log(window.code)
                  var countdown=60;
                  var validCode=true;
                  if(validCode){
                      validCode = false;
                      t=setInterval(function () {
                      countdown--;
                      obj.value = countdown+"秒后重新发送";
                      $('.j_button').removeAttr("onclick");
                      if (countdown == 0) {
                       clearInterval(t);
                       obj.value="重新发送";
                       $(obj).css('background','#00a8ee');
                       $('.j_button').attr("onclick", 'settime(this)');
                       validCode=true;
                      }
                     },1000);
                  }
              }else if(parseInt(res.status) == 0){
                  layer.tips('您还未注册!', '#j_tel');
              }
          }
      });
  }
}



function Confirm(){
    $('#j_btn').removeAttr("onclick");
    if(passStatus == 1 && telStatus == 1 && verifyStatus==1){
         var mydata=$("form[name=myform]").serialize();
           $.ajax({
                url:findpswd,
                type: 'post',
                data: mydata,
                headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
                dataType:'json',
                success: function(res){
                    if(res.status == 0){   //修改成功
                        window.location.href = gotourl;
                    }else if(res.status == 1){  //修改失败
                        layer.msg('修改失败!');
                    }
                }
        });
    }else{
        layer.msg('您填写的信息有误!');
    }
}
